﻿using System;
class ConvertBinToDec
{
    static int BinaryToDecimal(string number)
    {
        string temp = string.Empty;
        int res = 0;
        int[] binElements = new int[number.Length];
        for (int i = 0; i < number.Length; i++)
        {
            temp = string.Empty + number[i];
            binElements[i] = int.Parse(temp);
        }
        for (int j = 0; j < binElements.Length; j++)
        {
            if ((number[j] != '0') && (number[j] != '1'))
            {
                Console.WriteLine("it is not a BIN number");
                return 0;
            }
            res = res + (binElements[j] * ((int)Math.Pow(2, (binElements.Length - 1 - j))));
        }
        return res;
    }
    static string IntValidation()
    {
        string validatedNum;
        while (true)
        {
            try
            {
                int number = int.Parse(Console.ReadLine());
                validatedNum = number.ToString();
                return validatedNum;
            }
            catch (FormatException)
            {
                Console.Write("not in wanterd format. reenter: ");
            }
        }
    }
    static void Main()
    {
        Console.Write("enter bin number: ");
        string numberBin = IntValidation();
        int result = BinaryToDecimal(numberBin);
        Console.Write("the decimal representation of {0}, is: ", numberBin);
        Console.WriteLine(result);
        Console.ReadLine();
    }
}

